import { IDomEditor, ISelectMenu } from '@wangeditor/editor';

class InsertCellCulturePlate implements ISelectMenu {
  // 自带的字段
  title: string;
  tag: string;
  width: number;
  iconSvg?: string;
  // 自定义的字段
  defineTitle: object;
  dataName: string; //下拉框选项值对应editor实例中的字段名，通过editor[dataName]=[]设置值
  options: any;
  constructor(dataName: any) {
    this.title = 'select';
    this.tag = 'select';
    this.width = 110;
    this.dataName = dataName;
    this.defineTitle = {
      value: 'title',
      text: '细胞培养板',
      styleForRenderMenuList: { display: 'none' },
    };
  }
  // 下拉框的选项
  getOptions(editor: any) {
    // 这里我将下拉框选项的值存在了 editor 实例中，以便可以动态改变选项值
    const displayOptions = editor[this.dataName].data || [];
    this.options = [this.defineTitle, ...displayOptions];
    return this.options; // 这里返回的就是显示在下拉框选项上的值
  }
  // 菜单是否需要激活（如选中加粗文本，“加粗”菜单会激活），用不到则返回 false
  isActive(editor: IDomEditor): boolean {
    return false;
  }
  // 获取菜单执行时的 value ，用不到则返回空 字符串或 false
  getValue(editor: IDomEditor): string | boolean {
    return 'title'; // 为了不改变下拉框的标题，永远返回 'title'
  }
  // 菜单是否需要禁用（如选中 H1 ，“引用”菜单被禁用），用不到则返回 false
  isDisabled(editor: IDomEditor): boolean {
    return false;
  }
  // 点击菜单时触发的函数
  exec(editor: IDomEditor, value: string | boolean) {
    let node: any = {
      type: 'table',
      width: 'auto',
      children: [
        // {
        //   // 第一行
        //   type: "table-row",
        //   children: [{
        //     type: "table-cell",
        //     // isHeader: true,
        //     children: [{
        //       text: ""
        //     }]
        //   },
        // {
        //   type: "table-cell",
        //   // isHeader: true,
        //   children: [{
        //     text: ""
        //   }]
        // }]
        // },
        // {
        //   // 第二行
        //   type: "table-row",
        //   children: [{
        //     type: "table-cell",
        //     // isHeader: true,
        //     children: [{
        //       text: ""
        //     }]
        //   },
        //   {
        //     type: "table-cell",
        //     isHeader: true,
        //     children: [{
        //       text: ""
        //     }]
        //   }]
        // }
      ],
    };
    switch (value) {
      case '1536':
        node = drawTable(32, 48, node);
        break;
      case '368':
        node = drawTable(16, 24, node);
        break;
      case '96':
        node = drawTable(8, 12, node);
        break;
      case '48':
        node = drawTable(6, 8, node);
        break;
      case '24':
        node = drawTable(4, 6, node);
        break;
      case '12':
        node = drawTable(3, 4, node);
        break;
      case '6':
        node = drawTable(2, 3, node);
        break;
    }
    editor.insertNode(node);
  }
}

// 根据用户选择的细胞培养板格式创建表格
const drawTable = (row: number, col: number, node: any) => {
  for (let i = 0; i < row + 1; i++) {
    node.children.push({
      type: 'table-row',
      children: [],
    });
    for (let j = 0; j < col + 1; j++) {
      if (i == 0 && j != 0) {
        node.children[i].children.push({
          type: 'table-cell',
          children: [
            {
              text: `${j}`,
            },
          ],
        });
      } else if (j == 0 && i != 0) {
        node.children[i].children.push({
          type: 'table-cell',
          children: [
            {
              text: incrementString(
                String.fromCharCode('A'.charCodeAt(0) + i - 2),
              ),
            },
          ],
        });
      } else if (i == 0 && j == 0) {
        node.children[i].children.push({
          type: 'table-cell',
          isHeader: true,
          children: [
            {
              text: '',
            },
          ],
        });
      } else {
        node.children[i].children.push({
          type: 'table-cell',
          children: [
            {
              text: '',
            },
          ],
        });
      }
    }
  }
  return node;
};
// 将数字替换为字母
const incrementString = (str: string) => {
  // 将字符串转换为大写
  str = str.toUpperCase();

  // 将字符串转换为对应的数字
  let num = 0;
  for (let i = 0; i < str.length; i++) {
    num = num * 26 + (str.charCodeAt(i) - 'A'.charCodeAt(0) + 1);
  }
  // 将数字加1
  num++;
  // 将数字转换为相应的字母组合
  let result = '';
  while (num > 0) {
    const remainder = (num - 1) % 26;
    result = String.fromCharCode('A'.charCodeAt(0) + remainder) + result;
    num = Math.floor((num - 1) / 26);
  }
  return result;
};
export default InsertCellCulturePlate;
